﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Christoc.Modules.CouponsDiscounts.Components.Controllers;

namespace Christoc.Modules.CouponsDiscounts.Components
{
    public class ProductSale
    {
        private Product _product;
        private int _discountId;
        //private int _count;

        public int ProductID
        {
            get
            {
                return _product.ProductID;
            }
        }

        public string Name
        {
            get
            {
                return _product.Name;
            }
        }

        public double PriceReal
        {
            get
            {
                return _product.Price;
            }
        }

        public double PriceDiscountUnit { get; set; }

        public double PriceDiscountTotal { get; set; }

        public int DiscountId
        {
            get
            { return _discountId; }
            set
            {
                if (value < 0)
                {
                    _discountId = 0;
                }
                else
                {
                    _discountId = value;
                }
            }
        }
        /*
        public int Count 
        {
            set
            {
                //esto esta mal
                Discount discount = DiscountController.GetDiscount(_product.ProductID);

                if (discount.Type == TypeDiscount.Count && value >= discount.Min)
                {
                    double PriceDiscountUnitCount = PriceReal - (PriceReal * 100 / discount.Value);
                    PriceDiscountTotal = PriceDiscountUnitCount * value;
                    _count = value;
                }
            }
        }*/

        public ProductSale(Product product)
        {
            _product = product;
            DiscountId = ApplyDiscount();
        }

        public bool ApplyCoupon(int code)
        {
            Discount discount = DiscountController.GetDiscount(code);

            if (discount == null)
            {
                return false;
            }

            if (discount.Type != TypeDiscount.OneUse)
            {
                PriceDiscountUnit = PriceDiscountUnit - discount.Value;

                /*
                if (discount.Type == TypeDiscount.OneUse)
                {
                    discount.Using = true;
                    DiscountController.UpdateDiscount(discount);
                }*/
            }
            return true;
        }

        public int ApplyDiscount()
        {
            Discount discount = DiscountController.GetDiscountProduct(_product.ProductID);

            if (discount == null)
            {
                PriceDiscountUnit = PriceReal;
                return -1;
            }

            if (discount.Type == TypeDiscount.Amount)
            {
                PriceDiscountUnit = PriceReal - discount.Value;
            }
            else if (discount.Type == TypeDiscount.Percentage)
            {
                PriceDiscountUnit = PriceReal - (PriceReal * discount.Value / 100);
            }
            return discount.DiscountID;
        }
    }
}